<?php

    class orderinfoAction extends ZorderAction{

         public function index(){

		

		 //dump($_SESSION);

		 

		 if(!empty($_POST)){

		 

		 

		  

		   if($_POST['year']!=''){

		   

		    $where['year']=array('eq',$_POST['year']);

		   }

		   

		   if($_POST['month']!=''){

		   

		      $where['month']=array('eq',$_POST['month']);

		   }

		   

		   if($_POST['day']!=''){

		   

		      $where['day']=array('eq',$_POST['day']);

		   }

		   

		   $where['gid']=array('neq',"null");

		   

		   	$re=$this->page('orderinfo',10,'id desc',$where);

		    

			

			 $i=-1;

			 foreach($re as $v){

			 $i++;

			    

				$fenxiao=M('users')->where('id='.$v['fid'])->find();

				$re[$i]['flianxiren']=$fenxiao['lianxiren'];

				$re[$i]['fleixing']=$feixiao['leixing'];

				

				$daili=M('users')->where('id='.$v['did'])->find();

				

				$re[$i]['dlianxiren']=$daili['lianxiren'];

				

				 $chulishijian=$v['paytime']-$v['dotime'];

				

				$cshijian=$chulishijian/60;

				

				if($cshijian<1){

				  

				  $cshijian=$chulishijian.'秒';

				  

				}else{

				

				  $cshijian=floor($cshijian).'分钟'.($chulishijian%60).'秒';

				}

				

				$re[$i]['chulishijian']=$cshijian;

				

			 

			 

			 }

			

			

		 

		 }else{

		 

		 

		  $time= strtotime(date('Y-m-d'));

		  $where['gid']=array('neq',"null");

		  

         	$re=$this->page('orderinfo',10,'id desc',$where);

			

			 $i=-1;

			

			 foreach($re as $v){

			 $i++;

			    

				$fenxiao=M('users')->where('id='.$v['fid'])->find();

				

				

				$re[$i]['flianxiren']=$fenxiao['lianxiren'];

				$re[$i]['fleixing']=$fenxiao['leixing'];

				

				

				

				$daili=M('users')->where('id='.$v['did'])->find();

				

				$quanxian=M('users')->where('id='.$v['uid'])->field('quanxian')->find();

				

				$re[$i]['quanxian']=$quanxian['quanxian'];

				

				$re[$i]['dlianxiren']=$daili['lianxiren'];

				

			    $chulishijian=$v['paytime']-$v['dotime'];

				

				$cshijian=$chulishijian/60;

				

				if($cshijian<1){

				  

				  $cshijian=$chulishijian.'秒';

				  

				}else{

				

				  $cshijian=floor($cshijian).'分钟'.($chulishijian%60).'秒';

				}

				

				$re[$i]['chulishijian']=$cshijian;

				

				

			 

			 }

			

			

			

			//dump($this->re);

			}

			

			

		$y = date("Y");



		//获取当天的月份

		$m = date("m");

		

		//获取当天的号数

		$d = date("d");

			

		$start= mktime(0,0,0,$m,$d,$y);

		

	    $whereleixing['gid']=array('neq',"null");

		$whereleixing['status']=array('eq',"充值成功");	

		$whereleixing['dotime']=array('gt',$start);

		$jinrileixing=M('orderinfo')->where($whereleixing)->field('fid')->select();

		

		//dump($jinrileixing);

		$jhulianwang=0;

		$jshiti=0;

		foreach($jinrileixing as $jv){

		

		

		  

		  $leixing=M('users')->where('id='.$jv['fid'])->find();

		  

		  if($leixing['leixing']==0){

		    $jhulianwang=$jhulianwang+1;

		  }else{

		    $jshiti=$jshiti+1;

		  }

		 

		}

		

		$this->jshiti=$jshiti;

		$this->jhulianwang=$jhulianwang;

		

		

			$y = date("Y");



		//获取当天的月份

		$m = date("m");

		

		//获取当天的号数

		$d = date("d");

			

		$start= mktime(0,0,0,$m,$d,$y)-(86400*7);

		

	    $whereleixing['gid']=array('neq',"null");

		$whereleixing['status']=array('eq',"充值成功");	

		$whereleixing['dotime']=array('gt',$start);

		$jinrileixing=M('orderinfo')->where($whereleixing)->field('fid')->select();

		

		//dump($jinrileixing);

		$jhulianwang=0;

		$jshiti=0;

		foreach($jinrileixing as $jv){

		

		

		  

		  $leixing=M('users')->where('id='.$jv['fid'])->find();

		  

		  if($leixing['leixing']==0){

		    $jhulianwang=$jhulianwang+1;

		  }else{

		    $jshiti=$jshiti+1;

		  }

		 

		}

		

		$this->qshiti=$jshiti;

		$this->qhulianwang=$jhulianwang;

		

		

		

			

			

			//dump($re);

			$this->re=$re;

         	$this->display();

         }

		 

		 

		 public function gaibianzhuangtai(){

		  

		  if(!empty($_POST)){

		  

		  $_POST['chuliren']=session('adminname');

		  

		  $_POST['paytime']=time();

		  

		  $payinfo=M('orderinfo')->where('id='.$_POST['id'])->find();

		 //充值成功返利 

			  if($_POST['status']=="充值成功"){

			  

				 $this->sms($payinfo['tel'],"用户".$payinfo['tel']."您的充值已提交,预计30分钟内到账,个别24小时,请您耐心等待。客服028-62951555");

				 

				

				
                $good=M('goods')->where('id='.$payinfo['gid'])->find();
				
          
				//dump($quanxian['quanxian']);die;

			  

			  if($payinfo['fee']==$good['lingshou']){

			  

					if($payinfo['fid']!='' && $payinfo['good']!=''){

					 $finfo    = M('users')->where('id='.$payinfo['fid'])->find();

					 $ginfo    = M('goods')->where('id='.$payinfo['gid'])->find();

					 $addmoney = $ginfo['lingshou']-$ginfo['fenxiao'];

					 $tmoney['money'] = $finfo['money']+$addmoney;

					 M('users')->where('id='.$payinfo['fid'])->save($tmoney);

				  }

			  }

			  

			  if($payinfo['fee']!=$good['daili']){

			   if($payinfo['did']!='' && $payinfo['good']!=''){

				 $dinfo    = M('users')->where('id='.$payinfo['did'])->find();

				 $ginfo    = M('goods')->where('id='.$payinfo['gid'])->find();

				 $addmoney = $ginfo['fenxiao']-$ginfo['daili'];

				 $tmoney['money'] = $dinfo['money']+$addmoney;

				 M('users')->where('id='.$payinfo['did'])->save($tmoney);

			  }

		     }

		  

		  }

		  

		  //充值失败减去返利

		  

		  if($_POST['status']=="充值失败" or $_POST['status']=="订单退回"){

		  

		  

		   if($_POST['status']=="充值失败"){

		  

		   $this->sms($payinfo['tel'],"用户".$payinfo['tel']."您充值的因其它原因充值失败，已为您办理退款,预计1-5天到账,请致电028-62951555");

		   

		   }

		  

		  if($payinfo['status']=="充值成功"){

		  

		  

		  $this->sms($payinfo['tel'],"用户".$payinfo['tel']."您申请的充值退款已处理成功，预计1-5天到账，如有疑问请致电028-62951555");

		  

		  

		  }

		  

		  

		  if($payinfo['status']=="充值成功"){

		  

		     if($payinfo['fid']!='' && $payinfo['good']!=''){

				 $finfo    = M('users')->where('id='.$payinfo['fid'])->find();

				 $ginfo    = M('goods')->where('id='.$payinfo['gid'])->find();

				 $addmoney = $ginfo['lingshou']-$ginfo['fenxiao'];

				  $addmoney=(string)$addmoney;

				 $tmoney['money'] = $finfo['money']-$addmoney;

				 M('users')->where('id='.$payinfo['fid'])->save($tmoney);

			  }

			if($payinfo['did']!='' && $payinfo['good']!=''){

				 $dinfo    = M('users')->where('id='.$payinfo['did'])->find();

				 $ginfo    = M('goods')->where('id='.$payinfo['gid'])->find();

				

				 $addmoney1 = $ginfo['fenxiao']-$ginfo['daili'];

				 

				 $addmoney1=(string)$addmoney1;

				

				 $tmoney1['money'] = $dinfo['money']-$addmoney1;

				 // dump($tmoney1['money']);die;

				 M('users')->where('id='.$payinfo['did'])->save($tmoney1);

			  }

		    

		  

		  }

		  

		  

		  

		  

		  }

		  

		  

		  

		  

		  

		  

		   $re=M('orderinfo')->save($_POST);



           if($re!==false){

		    

			$ajax['msg']=1;

		   

		   }else{

		     

			 $ajax['msg']=2;

		   

		   }		

		   

		   

		   

		   if($_POST['status']=="充值成功") {

		     $ajax['zhuangtai']=3;

		   } 

		   if($_POST['status']=="充值失败") {

		     $ajax['zhuangtai']=4;

		   } 

		   if($_POST['status']=="订单退回") {

		     $ajax['zhuangtai']=5;

		   } 

		   

		   

		  }

		  

		  $ajax['chuliren']=session('adminname');

		  $ajax['time']=time();

		 $this->ajaxReturn($ajax);

		   

		 

		 }

		 

		 

		 public function fenlei(){

		 

		   if(!empty($_POST)){

		   

		     if($_POST['dingdanhao']!=''){

			 

			

			 

			    $where['orderno']=array('eq',$_POST['dingdanhao']);

				$where['gid']=array('neq',"null");

			    $this->listpage('orderinfo',10,'id desc',$where);

			 

			   

			 }else if($_POST['daili']!=''){

			 

			   $where['did']=array('eq',$_POST['daili']);

			   $where['gid']=array('neq',"null");

			   $this->listpage('orderinfo',10,'id desc',$where);

			  

			 

			 }else if($_POST['fenxiao']!=''){

			 

			    $where['fid']=array('eq',$_POST['fenxiao']);

				$where['gid']=array('neq',"null");

			   $this->listpage('orderinfo',10,'id desc',$where);

			   

			 }else if($_POST['tel']!=''){

			 

			   $where['tel']=array('eq',$_POST['tel']);  

			   $where['gid']=array('neq',"null");

			    $this->listpage('orderinfo',10,'id desc',$where);

				//last('orderinfo');

			 }

		   

		   

		   

		   

		   }

		 

		 

		   $this->display('index');

		 

		 

		 

		 }

		 

		 

		 

		 public function yichengjiao(){

		 

		   $where['status']=array('eq','充值成功');

		   $where['gid']=array('neq',"null");

		   

		   $this->listpage('orderinfo',10,'id desc',$where);

		   

		   $this->display('index');

		 

		 

		 }

		 

		 

		 

		 public function suoyou(){

		 

		 header("Content-type:application/vnd.ms-excel");



         header("Content-Disposition:filename=test.xls");

		 

		    $where['year']=array('eq',$_GET['year']);

			$where['month']=array('eq',$_GET['month']);

			$where['day']=array('eq',$_GET['day']);

		    $where['gid']=array('neq',"null");

		 

	

	

	

	   $re=M('orderinfo')->where($where)->select();

	  

	 

	

	 

	   echo '<table border=1>';

	   

	   echo '<tr>

     



                            <th>产品名称</th>

                            

                            <th>支付金额</th>

                            <th>电话号码</th>

							<th>购买时间</th>

							<th>状态</th>

	



                        </tr>';

	   foreach($re as $v){

	     

		 echo '<tr>

                        

                            <td class="tc">'.$v['good'].'</td>



                             <td>'.$v['fee'].'</td>

						   

						     <td>'.$v['tel'].'</td>

                             <td>'.date("Y-m-d h:i:s",$v['paytime']).'</td>

						     <td>'.$v['status'].'</td>



                        </tr>

';

	   

	   }

	 

	   echo '</table>';

	 



		 

		 

		 }

		 

		 

		 

		 public function yizhifu(){

		 

		 header("Content-type:application/vnd.ms-excel");



         header("Content-Disposition:filename=test.xls");

		 

		    $where['year']=array('eq',$_GET['year']);

			$where['month']=array('eq',$_GET['month']);

			$where['day']=array('eq',$_GET['day']);

		    $where['status']=array('eq','充值成功');

		 

	        $where['gid']=array('neq',"null");

	

	

	   $re=M('orderinfo')->where($where)->select();

	  

	  

	

	 

	   echo '<table border=1>';

	   

	   echo '<tr>

     



                            <th>产品名称</th>

                            

                            <th>支付金额</th>

                            <th>电话号码</th>

							<th>购买时间</th>

							<th>状态</th>

	



                        </tr>';

	   foreach($re as $v){

	     

		 echo '<tr>

                        

                            <td class="tc">'.$v['good'].'</td>



                             <td>'.$v['fee'].'</td>

						   

						     <td>'.$v['tel'].'</td>

                             <td>'.date("Y-m-d h:i:s",$v['paytime']).'</td>

						     <td>'.$v['status'].'</td>



                        </tr>

';

	   

	   }

	 

	   echo '</table>';

	 



		 

		 

		 }

		 

		 

		  public  function sms($tel='',$code=''){

      $ch = curl_init();



      curl_setopt($ch, CURLOPT_URL, "http://sms-api.luosimao.com/v1/send.json");



      curl_setopt($ch, CURLOPT_HTTP_VERSION  , CURL_HTTP_VERSION_1_0 );



      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);



      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);



      curl_setopt($ch, CURLOPT_HEADER, FALSE);



      curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);



      curl_setopt($ch, CURLOPT_USERPWD  , 'api:key-3ecf6b0e3baf4bf5dd3735d7261141ed');



      curl_setopt($ch, CURLOPT_POST, TRUE);



      curl_setopt($ch, CURLOPT_POSTFIELDS, array('mobile' => $tel,'message' => '尊敬的'.$code.'【人多多】'));



      $res = curl_exec( $ch );



      curl_close( $ch );



      $res = json_decode($res,true);



      return $res;



  }

  

		 

		 

    }